package com.stylefeng.guns.rest.modular.wz.api.v3;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.toolkit.IdWorker;
import com.stylefeng.guns.rest.common.base.controller.ApiBaseController;
import com.stylefeng.guns.rest.common.constant.Consts;
import com.stylefeng.guns.rest.common.persistence.dao.*;
import com.stylefeng.guns.rest.common.persistence.model.*;
import com.stylefeng.guns.rest.common.util.AliyunSmsUtil;
import com.stylefeng.guns.rest.common.util.StringUtils;
import com.stylefeng.guns.rest.domain.ParamMap;
import com.stylefeng.guns.rest.domain.Res;
import com.stylefeng.guns.rest.enums.*;
import com.stylefeng.guns.rest.modular.auth.util.JwtTokenUtil;
import com.stylefeng.guns.rest.modular.wz.cache.CacheService;
import com.stylefeng.guns.rest.modular.wz.controller.WzFinanceService;
import com.stylefeng.guns.rest.modular.wz.service.IPfFileService;
import com.stylefeng.guns.rest.modular.wz.service.IWzCouponService;
import com.stylefeng.guns.rest.modular.wz.service.IWzUserCouponService;
import com.stylefeng.guns.rest.modular.wz.util.AliOssUtil;
import com.stylefeng.guns.rest.modular.wz.util.AliPushUtil;
import com.stylefeng.guns.rest.modular.wz.util.wz.DateUtil;
import com.stylefeng.guns.rest.pay.method.PayMethod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @program: wz_dev_app_shop
 * @description: 券接口
 * @author: Chenchao
 * @create: 2019-03-04 15:10
 **/
@RestController
@RequestMapping("/wz/api/v3/coupon/")
public class WzCouponApi extends ApiBaseController {
    @Autowired
    private IWzCouponService couponService;
    @Autowired
    private CacheService cacheService;
    @Autowired
    private JwtTokenUtil jwtTokenUtil;
    @Autowired
    private WzCouponMapper couponMapper;
    @Autowired
    private IPfFileService pfFileService;
    @Autowired
    private IWzUserCouponService userCouponService;
    @Autowired
    private WzUserCouponMapper userCouponMapper;
    @Autowired
    private WzFinanceService financeService;
    @Autowired
    private WzCouponOrderMapper couponOrderMapper;
    @Autowired
    private WzUserInfoMapper userInfoMapper;
    @Autowired
    private WzAdvMapMapper advMapMapper;
    @Autowired
    private WzUserRebateMapper userRebateMapper;
    @Autowired
    private WzMapRebateMapper mapRebateMapper;
    @Autowired
    private WzCouponMarketMapper couponMarketMapper;
    @Autowired
    private WzMapMergeMapper mapMergeMapper;


    /**
     * 待送货
     */
    @PostMapping("waitDeliveryList")
    public Res waitDeliveryList(@RequestBody ParamMap paramMap) {
        String couponId = paramMap.getStrMisValid("couponId");
        //礼券
        String sql = "user_coupon_id As userCouponId,type,nick_name As nickName,avatar_url As avatarUrl,price,user_info_id As userInfoId";
        List<Map<String, Object>> userCouponList = userCouponMapper.selectMaps(new EntityWrapper<WzUserCoupon>().setSqlSelect(sql).eq("coupon_id", couponId).eq("status",EnWzCoupon.STATUS_Normal.getIndex()));
        return Res.success(userCouponList);
    }

    /**
     * 收货地址
     */
    @PostMapping("userInfo")
    public Res userInfo(@RequestBody ParamMap paramMap) {
        String userInfoId = paramMap.getStrMisValid("userInfoId");
        WzUserInfo userInfo = userInfoMapper.selectById(userInfoId);
        paramMap.put("merchantTel",userInfo.getMerchantTel());
        paramMap.put("merchantName",userInfo.getMerchantName());
        paramMap.put("merchantFulladdress",userInfo.getMerchantFulladdress());
        paramMap.put("merchantLatitude",userInfo.getMerchantLatitude());
        paramMap.put("merchantLongitude",userInfo.getMerchantLongitude());
        return Res.success(paramMap);
    }

    /**
     * 核销礼券
     */
    @PostMapping("cancellation")
    public Res cancellation(@RequestBody ParamMap paramMap) throws Exception{
        Long userCode = jwtTokenUtil.getUserCodeFromToken();
        Long  userCouponId = paramMap.getLongMisValid("userCouponId");
        WzUserCoupon userCoupon = userCouponMapper.selectById(userCouponId);//用户券模型
        WzCoupon coupon = couponMapper.selectById(userCoupon.getCouponId());

        if(!userCode.toString().equals(coupon.getUserCode().toString())){
            return Res.fail("此用户扫码无效!");
        }
        if(userCoupon != null){
            userCoupon.setScanUserCode(userCode.toString());
            if(userCoupon.getStatus() == EnWzUser.COUPON_STATUS_USED.getIndex()){
                return Res.fail("此券已使用!");
            }
            if(userCoupon.getBeginTime().after(new Date())){
                return Res.fail("请在有效期内使用!");
            }
            if(userCoupon.getEndTime().before(new Date())){
                return Res.fail("请在有效期内使用!");
            }
            WzUser scanUser = cacheService.user(userCode);
            WzUser user = cacheService.user(userCoupon.getUserCode());
            userCoupon.setNickName(user.getNickName());
            userCoupon.setAvatarUrl(user.getAvatarUrl());
            userCoupon.setStatus(EnWzUser.COUPON_STATUS_USED.getIndex());
            userCoupon.setScanTime(new Date());
            userCoupon.setScanName(scanUser.getNickName());
            boolean r = financeService.couponScan(userCoupon,coupon);
            if(r){
                return Res.success("核销成功!");
            }
        }
        return Res.fail("核销失败!");
    }



    /**
     * 券详情
     */
    @PostMapping("detail")
    public Res couponDetail(@RequestBody ParamMap paramMap) {
        String couponId = paramMap.getStrMisValid("couponId");
        Long userCode = jwtTokenUtil.getUserCodeFromToken();
        //礼券
        String sql = "user_code As userCode,logo,merchant_name As name,rule_type As ruleType,use_type As useType,price,coupon_id As couponId,adv_map_id As advMapId,rule,benefit,begin_time As beginTime,end_time As endTime,pic,reject_remark AS rejectRemark";
        Map<String, Object> coupon = couponService.selectMap(new EntityWrapper<WzCoupon>().setSqlSelect(sql).eq("coupon_id", couponId));
        WzAdvMap advMap = cacheService.advMap(Long.parseLong(coupon.get("advMapId").toString()));
        coupon.put("merchantTel", advMap.getMerchantTel());
        coupon.put("longitude", advMap.getLongitude());
        coupon.put("latitude", advMap.getLatitude());
        coupon.put("merchantAddress", advMap.getMerchantFullAddress());
        coupon.put("advMapEndTime", advMap.getEndTime());
        coupon.put("advMapShareLink",advMap.getShareLink());
        coupon.put("adCode",advMap.getAdCode());
        return Res.success(coupon);
    }

    /**
     * 新增券
     */
    @PostMapping("add")
    public Res addCoupon(@RequestBody ParamMap paramMap) throws Exception {
        Long userCode = jwtTokenUtil.getUserCodeFromToken();
        Long advMapId = paramMap.getLongMisValid("advMapId");//店铺id
        int price = paramMap.getIntMisValid("price");//单价
        String pic = paramMap.getStrMisValid("pic");//兑换物图片
        String benefit = paramMap.getStrMisValid("benefit");//优惠方案
        String beginTimeX = paramMap.getStrMisValid("beginTime");//礼券开始时间
        Date beginTime = DateUtil.toDateTime(beginTimeX + " 00:00:00");
        String endTimeX = paramMap.getStrMisValid("endTime");//礼券结束时间
        Date endTime = DateUtil.toDateTime(endTimeX + " 23:59:59");
        String rule = paramMap.getStrMisValid("rule");//使用规则
        int useType = paramMap.getIntMisValid("useType");//使用方式:0-到店消费;1-送货上门

        //advMap信息
        WzAdvMap advMap = advMapMapper.selectById(advMapId);
        if (beginTime.before(DateUtil.toDateTime(DateUtil.toString(new Date()) + " 00:00:00"))) {
            return Res.fail("开始时间不能早于今天！");
        }
        if (endTime.before(beginTime)) {
            return Res.fail("结束时间不能早于开始时间！");
        }
        if (endTime.after(advMap.getEndTime())) {
            return Res.fail("结束时间必须在店铺结束时间之前！");
        }

        WzCoupon coupon = new WzCoupon();
        coupon.setCouponId(IdWorker.getId());
        coupon.setPic(pic);
        coupon.setAdCode(advMap.getAdCode());
        coupon.setUserCode(userCode);
        coupon.setAdvMapId(advMapId.toString());
        coupon.setAdvMapCategoryFirst(advMap.getAdvMapCategoryFirst());
        coupon.setPrice(price);
        coupon.setBenefit(benefit);
        coupon.setRule(rule);
        coupon.setBeginTime(beginTime);
        coupon.setEndTime(endTime);
        coupon.setStatus(EnWzPrize.COUPON_STATUS_WaitCheck.getIndex());//审核中
        coupon.setCreateTime(new Date());
        coupon.setUseType(useType);
        coupon.setMerchantName(advMap.getName());
        coupon.setLogo(advMap.getLogo());
        boolean r = coupon.insert();
        if (r) {
            if (Consts.dev.equals("false")) {
                for (String c : Consts.AuditorTel) {
                    SendSmsResponse response = AliyunSmsUtil.sendAdminSms(c, "有新的礼券需要审核", Consts.ADMIN_TEMPLATE_CODE);
                }
            }
            return Res.success();
        }
        return Res.fail();
    }

    /**
     * 编辑券
     */
    @PostMapping("edit")
    public Res editCoupon(@RequestBody ParamMap paramMap) throws Exception {
        Long couponId = paramMap.getLongMisValid("couponId");
        WzCoupon coupon = couponMapper.selectById(couponId);
        if (coupon.getStatus() != EnWzPrize.COUPON_STATUS_Reject.getIndex()) {
            return Res.fail("当前状态无法修改！");
        }
        WzAdvMap advMap = advMapMapper.selectById(Long.parseLong(coupon.getAdvMapId()));
        String benefit = paramMap.getStrMisValid("benefit");//优惠方案
        String rule = paramMap.getStrMisValid("rule");//使用规则
        String beginTimeX = paramMap.getStrMisValid("beginTime");//礼券开始时间
        Date beginTime = DateUtil.toDateTime(beginTimeX + " 00:00:00");
        String endTimeX = paramMap.getStrMisValid("endTime");//礼券结束时间
        Date endTime = DateUtil.toDateTime(endTimeX + " 23:59:59");
        if (endTime.before(beginTime)) {
            return Res.fail("结束时间不能早于开始时间！");
        }
        if (endTime.after(advMap.getEndTime())) {
            return Res.fail("结束时间必须在店铺结束时间之前！");
        }
        int price = paramMap.getIntMisValid("price");//单价
        int useType = paramMap.getIntMisValid("useType");//使用方式:0-到店消费;1-送货上门

        String pic = paramMap.getStr("pic", "");//兑换物图片
        coupon.setPic(pic);
        coupon.setPrice(price);
        coupon.setBenefit(benefit);
        coupon.setRule(rule);
        coupon.setBeginTime(beginTime);
        coupon.setEndTime(endTime);
        coupon.setStatus(EnWzPrize.COUPON_STATUS_WaitCheck.getIndex());//审核中
        coupon.setUseType(useType);
        coupon.setRuleType(1);//规则是文字
        coupon.setMerchantName(advMap.getName());
        coupon.setLogo(advMap.getLogo());
        int r = couponMapper.updateById(coupon);
        if (r > 0) {
            if (Consts.dev.equals("false")) {
                for (String c : Consts.AuditorTel) {
                    SendSmsResponse response = AliyunSmsUtil.sendAdminSms(c, "有礼券修改需要审核", Consts.ADMIN_TEMPLATE_CODE);
                }
            }
            return Res.success();
        } else {
            return Res.fail();
        }
    }

    /**
     * 删除券
     */
    @PostMapping("delete")
    public Res deleteCoupon(@RequestBody ParamMap paramMap) throws Exception {
        Long couponId = paramMap.getLongMisValid("couponId");
        WzCoupon coupon = couponMapper.selectById(couponId);
        if (coupon.getStatus() != EnWzPrize.COUPON_STATUS_Reject.getIndex()) {
            return Res.fail("当前状态无法删除！");
        }
        String pic = coupon.getPic();
        if (pic.length() > 26) {
            pic = pic.substring(26, pic.length());
            PfFile file = pfFileService.selectOne(new EntityWrapper<PfFile>().eq("file_url", pic));
            if (file != null) {
                AliOssUtil.deleteObject(file.getFileUrl(), file.getType(), file.getBucketName());
                pfFileService.deleteById(file);
            }
        }
        int r=couponMapper.deleteById(couponId);
        if(r>0){
            return Res.success();
        }else {
            return Res.fail();
        }
    }

    /**
     * 券使用记录详情
     */
    @PostMapping("useRecord")
    public Res useRecord(@RequestBody ParamMap paramMap){
        int pageNum = paramMap.getIntMisValid("pageNum");
        int pageSize = paramMap.getIntMisValid("pageSize");
        int type = paramMap.getIntMisValid("type");//类型：1-原价券;2-;3-券库券;4-返利券;5-拼单券;
        Long couponId = paramMap.getLongMisValid("couponId");//券id

        Long mapMergeId = paramMap.getLongValid("mapMergeId");//拼单活动id
        Long mapActivityId = paramMap.getLongValid("mapActivityId");//享低价活动id
        Long mapRebateId = paramMap.getLongValid("mapRebateId");//返利活动id
        Long couponMarketId = paramMap.getLongValid("couponMarketId");//券库id
        EntityWrapper ew = new EntityWrapper<WzMapMerge>();
        ew.eq("coupon_id",couponId).eq("status", EnWzCoupon.STATUS_Used.getIndex()).eq("type",type);
        if(mapMergeId != null){
            ew.eq("map_merge_id",mapMergeId);
        }
        if(mapRebateId != null){
            ew.eq("map_rebate_id",mapRebateId);
        }
        if(mapActivityId != null){
            ew.eq("map_activity_id",mapActivityId);
        }
        if(couponMarketId != null){
            ew.eq("coupon_market_id",couponMarketId);
        }
        String sql = "market_rebate_profit As marketRebateProfit,price As profit,user_coupon_id As userCouponId,coupon_id As couponId,scan_time As scanTime,scan_name As scanName,nick_name As nickName,avatar_url As avatarUrl";
        List<Map<String, Object>> userCouponList = userCouponMapper.selectMapsPage(new Page<>(pageNum, pageSize),ew.setSqlSelect(sql));
        return Res.success(paramMap.data(userCouponList));
    }

    /**
     * 用户原价购买 Original price
     */
    @PostMapping("originalPriceBuy")
    public Res originalPriceBuy(@RequestBody ParamMap paramMap) throws Exception{
        Long userCode = jwtTokenUtil.getUserCodeFromToken();//下单用户编号
        Long couponId = paramMap.getLongMisValid("couponId");//券id
        int quantity = paramMap.getIntMisValid("quantity");//购买数量
        int amount = paramMap.getIntMisValid("amount");//前台计算价格
        int isRewardBalanceDeduction = paramMap.getIntMisValid("isRewardBalanceDeduction");//是否用积分余额抵扣：0-否;1-是
        int payMethod = paramMap.getIntMisValid("payMethod");//支付类型：1-支付宝;2-微信;

        WzUser user = cacheService.user(userCode);//用户信息
        WzCoupon coupon = couponMapper.selectById(couponId);//券信息

        //订单原金额计算
        int realAmount = quantity * coupon.getPrice();
        //生成用户原价订单数据
        WzCouponOrder couponOrder = new WzCouponOrder();
        couponOrder.setCouponOrderId(IdWorker.getId());
        couponOrder.setCouponId(couponId);
        couponOrder.setUserCode(user.getUserCode());
        couponOrder.setNickName(user.getNickName());
        couponOrder.setAvatarUrl(user.getAvatarUrl());
        couponOrder.setQuantity(quantity);
        couponOrder.setStatus(EnWzCoupon.COUPONORDER_STATUS_WaitPay.getIndex());//待支付
        couponOrder.setPayTime(DateUtil.addMinute(new Date(),30));//30分钟内支付
        couponOrder.setAmount(realAmount);//券原价
        couponOrder.setCreateTime(new Date());
        if(coupon.getUseType() == EnWzCoupon.UserType_Delivery.getIndex()){
            String userInfoId = paramMap.getStrMisValid("userInfoId");
            couponOrder.setUserInfoId(userInfoId);
        }
        //实际金额处理
        if(amount != realAmount){
            return Res.fail("金额错误!");
        }
        if(isRewardBalanceDeduction == 0){
            WzUserFinanceRecord userFinanceRecord = new WzUserFinanceRecord();//支付资金记录
            userFinanceRecord.setId(IdWorker.getId());
            userFinanceRecord.setAmount(realAmount);
            couponOrder.setCashAmount(realAmount);
            //数据保存处理
            boolean r = financeService.originalPriceCashBuy(paramMap,user,userFinanceRecord,couponOrder);
            //结果处理
            paramMap.put("couponOrderId",couponOrder.getCouponOrderId());
            if(r){
                //返回支付数据
                if(payMethod == EnWzUserFinanceRecord.payMethod_alipay.getIndex()){//支付宝支付
                    String str = PayMethod.aliAppPay(userFinanceRecord.getId().toString(),realAmount,"外赚-购买礼券订单");
                    paramMap.put("alipay",str);
                    return Res.success(paramMap);
                }else if(payMethod == EnWzUserFinanceRecord.payMethod_weixin.getIndex()){//微信支付
                    Map m = PayMethod.wxAppPay(realAmount,userFinanceRecord.getId().toString(),"外赚-购买礼券订单","8.8.8.8");
                    paramMap.put("weixin",m);
                    return Res.success(paramMap);
                }
            }
        }
        return Res.fail();
    }

    /**
     * 原价买券支付成功
     */
    @PostMapping("createMergeSuccess")
    public Res createMergeSuccess(@RequestBody ParamMap paramMap) throws Exception{
        Long couponOrderId = paramMap.getLongMisValid("couponOrderId");//用户原价券id
        WzCouponOrder couponOrder = couponOrderMapper.selectById(couponOrderId);
        WzCoupon coupon = cacheService.coupon(couponOrder.getCouponId());
        WzAdvMap advMap = cacheService.advMap(Long.parseLong(coupon.getAdvMapId()));
        paramMap.put("status",couponOrder.getStatus());
        String benefit = "【"+advMap.getName()+"】" + coupon.getBenefit();
        paramMap.put("benefit",benefit);
        paramMap.put("avatarUrl",couponOrder.getAvatarUrl());
        return Res.success(paramMap);
    }

    /**
     * 退款审核
     */
    @PostMapping("refundCheck")
    public Res refundCheck(@RequestBody ParamMap paramMap) throws Exception{
        Long userCouponId = paramMap.getLongMisValid("userCouponId");//用户券id
        WzUserCoupon userCoupon = userCouponMapper.selectById(userCouponId);
        if(userCoupon.getStatus() != EnWzCoupon.STATUS_Normal.getIndex()){
            return Res.fail("只有待使用的券才可以退款!");
        }
        if(userCoupon.getType() == EnWzCoupon.TYPE_Prize.getIndex() || StringUtils.isNotBlank(userCoupon.getPrizeId())){
            return Res.fail("奖品券不可以退款!");
        }
        if(userCoupon.getType() == EnWzCoupon.TYPE_OriginalPrice.getIndex()){//原价券
        }else if(userCoupon.getType() == EnWzCoupon.TYPE_Shopping.getIndex()){//券库券
            WzUserRebate userRebate = userRebateMapper.selectById(userCoupon.getUserRebateId());
            if(userRebate.getStatus() < EnWzMapRebate.USERREBATE_STATUS_END.getIndex()){
                return Res.fail("此券所在活动还未结束不可退款!");
            }
        }else if(userCoupon.getType() == EnWzCoupon.TYPE_Rebate.getIndex()){//返利券
            WzUserRebate userRebate = userRebateMapper.selectById(userCoupon.getUserRebateId());
            if(userRebate.getStatus() < EnWzMapRebate.USERREBATE_STATUS_END.getIndex()){
                return Res.fail("此券所在活动还未结束不可退款!");
            }
        }
        return Res.success();
    }

    /**
     * 退款
     */
    @PostMapping("refund")
    public Res refund(@RequestBody ParamMap paramMap) throws Exception{
        Long userCouponId = paramMap.getLongMisValid("userCouponId");//用户券id
        int type = paramMap.getIntValid("type");//用户券id
        WzUserCoupon userCoupon = userCouponMapper.selectById(userCouponId);
        if(userCoupon.getStatus() != EnWzCoupon.STATUS_Normal.getIndex()){
            return Res.fail("只有待使用的券才可以退款!");
        }
        if(userCoupon.getType() == EnWzCoupon.TYPE_Prize.getIndex() || StringUtils.isNotBlank(userCoupon.getPrizeId())){
            return Res.fail("奖品券不可以退款!");
        }
        int amount = 0;
        WzCouponMarket couponMarket = null;
        WzMapMerge mapMerge = null;
        WzMapRebate mapRebate = null;
        if(userCoupon.getType() == EnWzCoupon.TYPE_OriginalPrice.getIndex()){//原价券
            amount = userCoupon.getPrice();
        }else if(userCoupon.getType() == EnWzCoupon.TYPE_Shopping.getIndex()){//券库券
            WzUserRebate userRebate = userRebateMapper.selectById(userCoupon.getUserRebateId());
            if(userRebate.getStatus() < EnWzMapRebate.USERREBATE_STATUS_END.getIndex()){
                return Res.fail("活动还没结束不可以退款!");
            }
            //返利活动处理
            mapRebate = mapRebateMapper.selectById(userRebate.getMapRebateId());
            if(mapRebate.getStatus() == EnWzMapRebate.STATUS_END.getIndex() && mapRebate.getEndTime().after(new Date())){
                mapRebate.setStatus(EnWzMapRebate.STATUS_RELEASE.getIndex());
            }
            //券库活动处理
            couponMarket = couponMarketMapper.selectById(mapRebate.getCouponMarketId());
            couponMarket.setSurplusNumber(couponMarket.getSurplusNumber() + 1);//库存加
            if(couponMarket.getSurplusNumber() > 0 && couponMarket.getStatus() == EnWzCouponMarket.STATUS_END.getIndex() && couponMarket.getEndTime().after(new Date())){
                couponMarket.setStatus(EnWzMapMerge.STATUS_RELEASE.getIndex());
            }
            //退款金额计算
            amount = userCoupon.getPrice() + userCoupon.getMarketRebateProfit();
        }else if(userCoupon.getType() == EnWzCoupon.TYPE_Rebate.getIndex()){//返利券
            WzUserRebate userRebate = userRebateMapper.selectById(userCoupon.getUserRebateId());
            if(userRebate.getStatus() < EnWzMapRebate.USERREBATE_STATUS_END.getIndex()){
                return Res.fail("活动还没结束不可以退款!");
            }
            //返利活动处理
            mapRebate = mapRebateMapper.selectById(userRebate.getMapRebateId());
            if(mapRebate.getStatus() == EnWzMapRebate.STATUS_END.getIndex() && mapRebate.getEndTime().after(new Date())){
                mapRebate.setStatus(EnWzMapRebate.STATUS_RELEASE.getIndex());
            }
            //退款金额计算
            amount = userCoupon.getPrice();
        }else if(userCoupon.getType() == EnWzCoupon.TYPE_Merge.getIndex()){//拼单券
            //拼单活动处理
            mapMerge = mapMergeMapper.selectById(Long.parseLong(userCoupon.getMapMergeId()));
            mapMerge.setSurplusNumber(mapMerge.getSurplusNumber() + 1);
            if(mapMerge.getSurplusNumber() > 0 && mapMerge.getStatus() == EnWzMapMerge.STATUS_END.getIndex() && mapMerge.getEndTime().after(new Date())){
                mapMerge.setStatus(EnWzMapMerge.STATUS_RELEASE.getIndex());
            }
            //退款金额计算
            amount = userCoupon.getPrice();
        }

        if(userCoupon.getPrice() < 10 && userCoupon.getPayMethod() == EnWzUserFinanceRecord.payMethod_alipay.getIndex()){
            boolean r = financeService.refundProfit(userCoupon,amount,couponMarket,mapMerge,mapRebate);
            if(r){
                return Res.success("已退款到您的积分余额!");
            }else{
                return Res.fail("退款失败，请重试!");
            }
        }else if(userCoupon.getPrice() < 100 && userCoupon.getPayMethod() == EnWzUserFinanceRecord.payMethod_weixin.getIndex()){
            boolean r = financeService.refundProfit(userCoupon,amount,couponMarket,mapMerge,mapRebate);
            if(r){
                return Res.success("已退款到您的积分余额!");
            }else{
                return Res.fail("退款失败，请重试!");
            }
        }else{
            boolean r = financeService.refund(userCoupon,amount,couponMarket,mapMerge,mapRebate);
            if(r){
                return Res.success("退款成功!");
            }else{
                return Res.fail("退款失败，请重试!");
            }
        }
    }
}
